package com.fozu.office.system.web;
/*
 * 员工管理
 */
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fozu.common.codec.MD5Util;
import com.fozu.common.config.Constants;
import com.fozu.common.pojo.ResultVO;
import com.fozu.common.util.IdGen;
import com.fozu.common.util.StringUtil;
import com.fozu.common.util.ValidateUtil;
import com.fozu.office.system.entity.UserVO;
import com.fozu.office.system.service.UserServiceImpl;

@Controller
@RequestMapping(value="user")
public class UserController {
	@Autowired
	private UserServiceImpl userService;
	
	//跳转到用户列表页面
	@RequestMapping(value="userList.shtml")
	public String toUserList(){
		return "system/user/userList";
	}
	//跳转-->添加页面
	@RequestMapping(value="userAdd.shtml")
	public String toUserAdd(){
		return "system/user/userAdd";
	}
	//跳转到用户修改页面
	@RequestMapping(value="userUpdate.shtml")
	public String toUserUpdate(){
		return "system/user/userUpdate";
	}	
	
	//登录  async=false 改变ajax异步请求为同步
	@PostMapping(value="login")
	@ResponseBody
	public ResultVO login(UserVO user,HttpServletRequest request){
		try{
			if(StringUtil.isEmpty(user.getUserName())){
				return new ResultVO(Constants.status_FAILED,"用户名不能为空");
			}
			if(StringUtil.isEmpty(user.getPassword())){
				return new ResultVO(Constants.status_FAILED,"密码不能为空");
			}
				
			UserVO us = userService.login(user);
			if(us==null){
				return new ResultVO(Constants.status_FAILED,"账号不存在",null);
			}
			if(!MD5Util.md5Encode(user.getPassword()).equals(us.getPassword())){
				return new ResultVO(Constants.status_FAILED,"密码错误",null);
			}
			if(us.getStatus()==1){
				return new ResultVO(Constants.status_FAILED,"该账号已被暂停服务",null);
			}
			if(us.getDelFlag()==1){
				return new ResultVO(Constants.status_FAILED,"该账号已被删除",null);
			}
			request.getSession().setAttribute("loginUser", us);
			return new ResultVO(Constants.status_SUCCESS,"登录成功",us);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_Exception,"系统异常",null);
		}
	}
	//退出
	@PostMapping(value="logout")
	@ResponseBody
	public ResultVO loginOut(HttpServletRequest request){
		try{
			request.getSession().removeAttribute("loginUser");
			return new ResultVO(Constants.status_SUCCESS,"退出成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"退出失败",null);
		}
		
	}
	
	
	//用户列表(分页)
	@PostMapping(value="getUserList")
	@ResponseBody
	public ResultVO getUserList(@RequestParam(value="pageNo",required=true) Long pageNo,
								@RequestParam(value="pageSize",required=true) Long pageSize,
								@RequestParam(value="userName",required=false) String userName,
								@RequestParam(value="mobile",required=false) String mobile,
								@RequestParam(value="status",required=false,defaultValue="-1") Integer status
								){
		try{
			UserVO user = new UserVO(userName,mobile,status);
			IPage<UserVO> page = userService.listUser(new Page<UserVO>(pageNo,pageSize),user);
			/*Long current= page.getCurrent();   //当前页
			Long pages = page.getPages();        //页数
			Long total = page.getTotal();        //条数
			List userList = page.getRecords();   //数据集合
			System.out.println(userList.size());*/
			return new ResultVO(page);
		}catch(Exception e){ 
			e.printStackTrace();
			return new ResultVO(Constants.status_Exception,"系统异常",null); 
		}
	}
	
	//做添加操作
	@PostMapping(value="doAddUser")
	@ResponseBody
	public ResultVO doAddUser(UserVO user,HttpServletRequest request){
		try{
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			user.setId(IdGen.getId());
			user.setPassword(MD5Util.md5Encode(user.getPassword()));
			user.setCreateDate(new Date());
			user.setUpdateDate(new Date());
			user.setCreateBy(loginUser.getId());
			user.setUpdateBy(loginUser.getId());
			userService.addUser(user);
			return new ResultVO(Constants.status_SUCCESS,"添加成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"添加失败");
		}
	}
	//根据id删除user(批量+单个)
	@PostMapping(value="deleteUsers")
	@ResponseBody
	public ResultVO deleteUsers(String ids){
		try{
			String [] userIds = ids.split(",");
			userService.delUserByIds(userIds);
			return new ResultVO(Constants.status_SUCCESS,"删除成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"删除失败",null);
		}
		
		
	}
	
	//根据id获得user对象
	@PostMapping(value="getUserById")
	@ResponseBody
	public ResultVO getUserById(@RequestParam(value="userId") String userId){
		try{
			UserVO user = new UserVO();
			user.setId(userId);
			UserVO us=userService.getUserById(user);
			return new ResultVO(us);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"获取失败",null);
		}
	}
	
	//修改用户信息
	@PostMapping(value="updateUser")
	@ResponseBody
	public ResultVO updateUser(HttpServletRequest request,
							   @RequestParam(value="userId",required=true) String userId,
							   @RequestParam(value="nickName",required=true) String nickName,
							   @RequestParam(value="mobile",required=true) String mobile,
							   @RequestParam(value="email",required=true) String email,
							   @RequestParam(value="password") String password,
							   @RequestParam(value="confirmPass") String confirmPass,
							   @RequestParam(value="sex" ,required=true) Integer sex){
		try{
			UserVO loginUser = (UserVO) request.getSession().getAttribute("loginUser");
			UserVO user = new UserVO(userId,nickName,email,mobile,sex);
			if(ValidateUtil.validConfirmPwd(password, confirmPass)){
				user.setPassword(MD5Util.md5Encode(password));
			}
			user.setUpdateBy(loginUser.getId());
			user.setUpdateDate(new Date());
			userService.updateUser(user);
			return new ResultVO(Constants.status_SUCCESS,"修改成功",null);
		}catch (Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"修改失败",null);
		}
	}
	//修改用户状态
	@PostMapping(value="updateStatus")
	@ResponseBody
	public ResultVO updateStatus(@RequestParam("userId") String userId,
								 @RequestParam("status") Integer status){
		try{
			if(StringUtil.isEmpty(userId)){
				return new ResultVO(Constants.status_FAILED,"用户ID非法",null);
			}
			if(!(status==0||status==1)){
				return new ResultVO(Constants.status_FAILED,"状态非法",null);
			}
			UserVO user = new UserVO();
			user.setId(userId);
			user.setStatus(status);
			userService.updateStatus(user);
			return new ResultVO(Constants.status_SUCCESS,"修改成功",null);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_FAILED,"修改失败",null);
		}
	}
	//获取用户姓名、id
	@PostMapping(value="getUserIds")
	@ResponseBody
	public ResultVO getUserIds(){
		try{
			List userIds = userService.getUserIds();
			return new ResultVO(userIds);
		}catch (Exception e){
			e.printStackTrace();
			return new ResultVO(Constants.status_SUCCESS,"获取失败",null);
		}
	}
}
